<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<div>
    <h1>微博首页</h1>
    <div v-if="isLogin"><!-- 已登录 -->
        <h3>欢迎{{user.nickname}}回来</h3>
        <a href="/insert.html">发微博</a>
        <a href="javascript:void (0)" @click="logout()">退出登录</a>
    </div>
    <div v-else><!-- 未登录 -->
        <a href="/reg.html">注册</a>
        <a href="/login.html">登录</a>
    </div>
    <br>
    <div v-for="w in arr">
        <h3>
            <a :href="'/detail.html?id='+w.id">
                {{w.nickname + "：" + w.content}}
            </a>
        </h3>
    </div>
</div>
<!-- 引入axios 和 vue框架文件 -->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<script>
    let v = new Vue({
        el: "body>div",
        data: {
            isLogin: false,
            user: {},
            arr: []
        },
        methods: {
            logout() {
                if (confirm("您确认退出登录吗？")) {
                    axios.get("/logout").then(function (response) {
                        v.isLogin = false;
                    })
                }
            }
        },
        created: function () {
            /* 发请求获取当前登录的用户对象 */
            axios.get("/currentUser").then(function (response) {
                v.user = response.data;
                /* if (response.data==""){
                        v.isLogin = false;
                    }else {
                        v.isLogin=true;
                    } 第一种写法

                    第二种写法
                    v.isLogin = response.data == ""?false:true;
                     */
                v.isLogin = !response.data == "";
            })
            /* 发请求获取所有微博数据 */
            axios.get("/weibo/selectIndex").then(function (response) {
                v.arr = response.data;
            })
        }
    })
</script>
</body>
</html>